package com.itany.shop.service.proxy;

import com.itany.shop.entity.Order;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.ServiceException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.OrderService;
import com.itany.shop.tran.TransactionManager;
import com.itany.shop.vo.Cart;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/19 10:33
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class OrderServiceProxy implements OrderService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderServiceTarget");

    @Override
    public String createOrder(Cart cart, User user) throws UserNotLoginException, ServiceException {
        try {
            tran.begin();
            String no = orderService.createOrder(cart,user);
            tran.commit();
            return no;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Order> findMyOrders(User user) throws ServiceException, UserNotLoginException {
        try {
            tran.begin();
            List<Order> orders = orderService.findMyOrders(user);
            tran.commit();
            return orders;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public Order findDetail(String id) throws RequestParameterErrorException, ServiceException {
        try {
            tran.begin();
            Order order = orderService.findDetail(id);
            tran.commit();
            return order;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        }
    }
}
